package online.yysh.charging.zxing;

import android.app.Activity;
import android.content.Intent;
import android.os.Message;

import com.google.zxing.Result;
import online.yysh.charging.ScanAndChargeActivity;
import online.yysh.charging.utils.Constant;

import android.os.Handler;

public final class ScanAndChargeActivityHandler extends Handler {

    private static final String TAG = ScanAndChargeActivityHandler.class.getSimpleName();

    private final ScanAndChargeActivity activity;
    private final DecodeThread decodeThread;
    private ScanAndChargeActivityHandler.State state;
    private final CameraManager cameraManager;

    private enum State {
        PREVIEW, SUCCESS, DONE
    }

    public ScanAndChargeActivityHandler(ScanAndChargeActivity activity, CameraManager cameraManager) {
        this.activity = activity;
        decodeThread = new DecodeThread(activity, new ViewfinderResultPointCallback(
                activity.getViewfinderView()));
        decodeThread.start();
        state = ScanAndChargeActivityHandler.State.SUCCESS;

        // Start ourselves capturing previews and decoding.
        // 开始拍摄预览和解码
        this.cameraManager = cameraManager;
        cameraManager.startPreview();
        restartPreviewAndDecode();
    }

    @Override
    public void handleMessage(Message message) {
        switch (message.what) {
            case Constant.RESTART_PREVIEW:
                // 重新预览

                restartPreviewAndDecode();
                break;
            case Constant.DECODE_SUCCEEDED:
                // 解码成功

                state = ScanAndChargeActivityHandler.State.SUCCESS;
                activity.handleDecode((Result) message.obj);

                break;
            case Constant.DECODE_FAILED:

                // 尽可能快的解码，以便可以在解码失败时，开始另一次解码

                state = ScanAndChargeActivityHandler.State.PREVIEW;
                cameraManager.requestPreviewFrame(decodeThread.getHandler(),
                        Constant.DECODE);
                break;
            case Constant.RETURN_SCAN_RESULT:

                activity.setResult(Activity.RESULT_OK, (Intent) message.obj);
                activity.finish();
                break;
//            case Constant.FLASH_OPEN:
//                activity.switchFlashImg(Constant.FLASH_OPEN);
//                break;
//            case Constant.FLASH_CLOSE:
//                activity.switchFlashImg(Constant.FLASH_CLOSE);
//                break;
        }
    }

    /**
     * 完全退出
     */
    public void quitSynchronously() {
        state = ScanAndChargeActivityHandler.State.DONE;
        cameraManager.stopPreview();
        Message quit = Message.obtain(decodeThread.getHandler(), Constant.QUIT);
        quit.sendToTarget();
        try {
            // Wait at most half a second; should be enough time, and onPause()
            // will timeout quickly
            decodeThread.join(500L);
        } catch (InterruptedException e) {
            // continue
        }

        // Be absolutely sure we don't send any queued up messages
        //确保不会发送任何队列消息
        removeMessages(Constant.DECODE_SUCCEEDED);
        removeMessages(Constant.DECODE_FAILED);
    }

    public void restartPreviewAndDecode() {
        if (state == ScanAndChargeActivityHandler.State.SUCCESS) {
            state = ScanAndChargeActivityHandler.State.PREVIEW;
            cameraManager.requestPreviewFrame(decodeThread.getHandler(),
                    Constant.DECODE);
            activity.drawViewfinder();
        }
    }

}
